#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   wconnect.py
@Time    :   2020/07/04 18:28:40
@Author  :   Wicos 
@Version :   1.0
@Contact :   wicos@wicos.cn
'''

# here put the import lib
from fastapi import FastAPI, WebSocket
from queue import Queue
app = FastAPI()

msg = Queue()
msg_list = []

@app.get("/")
def index():
    main_data = {"name": "wconnect"}
    return main_data

# receive_bytes receive_text receive_json
"""
[{"topic":"topic_one","data":"send data"},{"topic":"topic_two","data":"send data"}]
{"topic_one":[user_ids],"topic_two":[user_ids]}
"""
@app.websocket("/ws/{user_id}")
async def ws(websocket: WebSocket,user_id:str=None):
    await websocket.accept()
    if user_id and type(int(user_id)) is int:
        await websocket.send_json({"id":user_id,"topic":"None"})
    while True:
        data = await websocket.receive_text()
        #await websocket.send_text(f"Message text was: {msg.get()}")
        await websocket.send_text(f"Message text was: {msg_list[-1]}")

@app.get("/recv/{recv}")
def send(recv):
    #msg.put(send)
    msg_list.append(send)
    return {"msg":"发送成功","data":send}
